﻿package net.wangit.view.element.component;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.enumeration.EnumerationFactory;

public class LinkedSelectElementWriter extends SelectElementWriter {

	public static final String SHOWMODE_SUFFIX = ".42.m";

	public LinkedSelectElementWriter() {
	}

	public void write(ActionContext ac, StringBuffer html, ElementComponent ec,
			Row row) {
		String enumerationId = (String) ec.getObject();
		MapList enumeration = null;
		if (!Checker.isEmpty(enumerationId)) {
			String domain = ec.getParent().getDomain();
			enumeration = EnumerationFactory.getEnumeration(ac, domain,
					enumerationId);
			ec.setObject(enumeration);
		}
		String name = ElementUtil.getAlias(ec);
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		if (Checker.isEmpty(value) && !Checker.isEmpty(enumeration))
			value = enumeration.getRow(0).get("value");
		ac.setRequestParameter(name, value);
		int showMode = ec.getShowMode();
		html.append("<input name=\"").append(name).append(".42.m").append(
				"\" type=hidden value=\"").append(showMode).append("\"/>");
		html.append("<span id=\"").append(name).append("\">");
		super.write(ac, html, ec, row);
		html.append("</span>");
	}
}
